﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Windsor;
using Microsoft.Practices.ServiceLocation;
using NHibernate.Cfg;
using RhinoSecurity = Rhino.Security;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using Tanmia.Basis.Data.NHibernate;
using Castle.MicroKernel.Registration;
using Rhino.Security.Interfaces;
using Rhino.Security;
using Rhino.Security.Model;

namespace Tanmia.Basis.Security.Adapter
{
    public class SecurityAdapterInitializer
    {
        public static void Init(DatabaseConfiguration dbConfig)
        {
            RhinoSecurity.Security.Configure<UserAdapter>(dbConfig.Config, RhinoSecurity.SecurityTableStructure.Prefix);
        }

        public static void ConfigureContainer(DatabaseConfiguration dbConfig, IWindsorContainer container)
        {
            container.Register(Component
                .For<RhinoSecurity.Interfaces.IAuthorizationService>()
                .ImplementedBy<RhinoSecurity.Services.AuthorizationService>()
                .LifeStyle.Transient);

            container.Register(Component
                .For<RhinoSecurity.Interfaces.IAuthorizationRepository>()
                .ImplementedBy<RhinoSecurity.Services.AuthorizationRepository>()
                .LifeStyle.Transient);

            container.Register(Component
                .For<RhinoSecurity.Interfaces.IPermissionsBuilderService>()
                .ImplementedBy<RhinoSecurity.Services.PermissionsBuilderService>()
                .LifeStyle.Transient);

            container.Register(Component
                .For<RhinoSecurity.Interfaces.IPermissionsService>()
                .ImplementedBy<RhinoSecurity.Services.PermissionsService>()
                .LifeStyle.Transient);

            container.Register(Component
                .For<Tanmia.Basis.Security.IAuthenticationService>()
                .ImplementedBy<DatabaseAuthenticationService>()
                .LifeStyle.Singleton);

            container.Register(Component
                .For<Tanmia.Basis.Security.IAuthorizationService>()
                .ImplementedBy<AuthorizationService>()
                .LifeStyle.Singleton);

            ISession session = dbConfig.SessionFactory.OpenSession();
            container.Register(Component.For<ISession>().Instance(session));
        }

        public static void SetupOperation(IWindsorContainer container)
        {
            IAuthorizationRepository authorizationRepo = container.Resolve<IAuthorizationRepository>();
            ISession session = container.Resolve<ISession>();

            //authorizationRepo.CreateOperation("/WarehouseModule/UnitEditor");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/UnitBrowser");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/Unit/Add");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/Unit/Edit");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/Unit/Delete");
            //session.Flush();

            //authorizationRepo.CreateOperation("/WarehouseModule/BranchEditor");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/BranchBrowser");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/Branch/Add");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/Branch/Edit");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/Branch/Delete");
            //session.Flush();

            //authorizationRepo.CreateOperation("/WarehouseModule/StoreEditor");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/StoreBrowser");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/Store/Add");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/Store/Edit");
            //session.Flush();
            //authorizationRepo.CreateOperation("/WarehouseModule/Store/Delete");
            //session.Flush();
        }

        public static void SetupPermission(IWindsorContainer container)
        {
            IPermissionsBuilderService permissionBuilder = container.Resolve<IPermissionsBuilderService>();
            IAuthenticationService authenticationService = container.Resolve<IAuthenticationService>();
            ISession session = container.Resolve<ISession>();

            //permissionBuilder.Allow("/WarehouseModule").For(authenticationService.User as IUser).OnEverything().DefaultLevel().Save();
            //session.Flush();
            //permissionBuilder.Allow("/WarehouseModule/UnitEditor").For(authenticationService.User as IUser).OnEverything().DefaultLevel().Save();
            //session.Flush();
            //permissionBuilder.Allow("/WarehouseModule/UnitBrowser").For(authenticationService.User as IUser).OnEverything().DefaultLevel().Save();
            //session.Flush();

            //permissionBuilder.Allow("/WarehouseModule/BranchEditor").For(authenticationService.User as IUser).OnEverything().DefaultLevel().Save();
            //session.Flush();
            //permissionBuilder.Allow("/WarehouseModule/BranchBrowser").For(authenticationService.User as IUser).OnEverything().DefaultLevel().Save();
            //session.Flush();

            //permissionBuilder.Allow("/WarehouseModule/StoreEditor").For(authenticationService.User as IUser).OnEverything().DefaultLevel().Save();
            //session.Flush();
            //permissionBuilder.Allow("/WarehouseModule/StoreBrowser").For(authenticationService.User as IUser).OnEverything().DefaultLevel().Save();
            //session.Flush();
        }

    }
}
